package net.toby200.canalbuddy.notices.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import net.toby200.canalbuddy.notices.model.Notice;

import java.util.Collection;
import java.util.logging.Logger;

/**
 * Created by toby on 29/12/2014.
 */
public class NoticeCache {
    private static final Logger LOG = Logger.getLogger(NoticeCache.class.getName());

    private static DaoSession daoSession;
    private static NoticeDao noticeDao;

    private static void setupDatabase(Context context) {
        if (daoSession != null) {
            return;
        }
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "notice-db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noticeDao = daoSession.getNoticeDao();
    }


    public static void saveNotice(Notice notice) {
        noticeDao.insertOrReplace(notice);
    }

    public static void saveNotices(Collection<Notice> notices) {
        noticeDao.insertOrReplaceInTx(notices);
    }

    public static Collection<Notice> loadNotices() {
        return noticeDao.loadAll();
    }

    public static void deleteAll() {
        noticeDao.deleteAll();
    }

    public static void deleteNotices(Collection<Notice> notices) {
        noticeDao.deleteInTx(notices);
    }

    public static void deleteNotice(Notice notice) {
        noticeDao.delete(notice);
    }

}